#!/usr/bin/env python
# -*- coding: utf-8 -*-

import	sys
import	time

def	test_add_1(length, times) :
	for i in xrange(times) :
		s = ''
		for k in xrange(length) :
			s = s + 'a'
	# test_add

def	test_add_2(length, times) :
	for i in xrange(times) :
		s = ''
		for k in xrange(length) :
			s = 'a' + s
	# test_add

def	test_format_1(length, times) :
	for i in xrange(times) :
		s = ''
		for k in xrange(length) :
			s = '%s%s' % (s, 'a')
	# test_format_1

def	test_multiply(length, times) :
	for i in xrange(times) :
		s = 'a' * length
	# test_multiply

def	test_join_1(length, times) :
	l = []
	for k in xrange(length) :
		l.append( 'a' )
	for i in xrange(times) :
		s = ''.join(l)
	# test_join_1()

def	test_join_2(length, times) :
	for i in xrange(times) :
		l = []
		for k in xrange(length) :
			l.append( 'a' )
		s = ''.join(l)
	# test_join_1()

def	test_cstringio(length, times) :
	from cStringIO import StringIO
	for i in xrange(times) :
		o = StringIO()
		for k in xrange(length) :
			o.write('a')
		s = o.getvalue()
		o.close()
	# test_cstringio()

def	test_stringio(length, times) :
	from StringIO import StringIO
	for i in xrange(times) :
		o = StringIO()
		for k in xrange(length) :
			o.write('a')
		s = o.getvalue()
		o.close()
	# test_stringio()

def	main() :
	times = 10000
	length = 8192
	
	time_start = time.time()
	test_multiply(length, times)
	time_stop = time.time()
	time_last = time_stop - time_start
	tps = times / time_last
	print 'Method Multiply : times=%d string-length=%d time-last=%.2f seconds tps=%.2f' % (times, length, time_last, tps)

	time_start = time.time()
	test_join_1(length, times)
	time_stop = time.time()
	time_last = time_stop - time_start
	tps = times / time_last
	print 'Method Join 1 : times=%d string-length=%d time-last=%.2f seconds tps=%.2f' % (times, length, time_last, tps)

	time_start = time.time()
	test_join_2(length, times)
	time_stop = time.time()
	time_last = time_stop - time_start
	tps = times / time_last
	print 'Method Join 2 : times=%d string-length=%d time-last=%.2f seconds tps=%.2f' % (times, length, time_last, tps)

	time_start = time.time()
	test_cstringio(length, times)
	time_stop = time.time()
	time_last = time_stop - time_start
	tps = times / time_last
	print 'Method cStringIO : times=%d string-length=%d time-last=%.2f seconds tps=%.2f' % (times, length, time_last, tps)

	time_start = time.time()
	test_stringio(length, times)
	time_stop = time.time()
	time_last = time_stop - time_start
	tps = times / time_last
	print 'Method StringIO : times=%d string-length=%d time-last=%.2f seconds tps=%.2f' % (times, length, time_last, tps)

	time_start = time.time()
	test_add_1(length, times)
	time_stop = time.time()
	time_last = time_stop - time_start
	tps = times / time_last
	print 'Method Add 1 : times=%d string-length=%d time-last=%.2f seconds tps=%.2f' % (times, length, time_last, tps)

	time_start = time.time()
	test_format_1(length, times)
	time_stop = time.time()
	time_last = time_stop - time_start
	tps = times / time_last
	print 'Method Format 1 : times=%d string-length=%d time-last=%.2f seconds tps=%.2f' % (times, length, time_last, tps)

'''
	time_start = time.time()
	test_add_2(length, times)
	time_stop = time.time()
	time_last = time_stop - time_start
	tps = times / time_last
	print 'Method Add 2 : times=%d string-length=%d time-last=%.2f seconds tps=%.2f' % (times, length, time_last, tps)
'''

	# main()

if	__name__ == '__main__' :
	main()
	sys.exit(0)

